<template>
  <div class="app-container">
    <div class="title-icon">
      <i class="iconfont iconhelp" @click="preview"></i>
    </div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="流程名称" prop="workflowName">
        <el-input
          v-model="queryParams.workflowName"
          placeholder="请输入流程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程标识" prop="workflowCode">
        <el-input
          v-model="queryParams.workflowCode"
          placeholder="请输入流程标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="全部" value />
          <el-option label="停用" value="0" />
          <el-option label="正常" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:workflowConfig:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:workflowConfig:edit']"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:workflowConfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:workflowConfig:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table
      stripe
      v-loading="loading"
      :data="workflowConfigList"
      @sort-change="tableSortChange"
      @selection-change="handleSelectionChange"
      :default-sort="{prop: 'status', order: 'descending'}"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="流程名称"
        align="left"
        prop="workflowName"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="流程链接"
        align="left"
        prop="url"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="流程标识"
        align="center"
        prop="workflowCode"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status === '0' ? 'info' : 'success'"
            disable-transitions
          >{{scope.row.statusText}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="left"
        prop="remark"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <!--<el-table-column label="guid" align="center" prop="guid" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:workflowConfig:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:workflowConfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="id 主键" prop="id">
          <el-input v-model="form.id" placeholder="请输入id 主键" />
        </el-form-item>-->
        <el-form-item label="流程名称" prop="workflowName">
          <el-input v-model="form.workflowName" placeholder="请输入流程名称" />
        </el-form-item>
        <el-form-item label="流程链接" prop="url">
          <el-input v-model="form.url" type="textarea" placeholder="请输入流程链接" />
        </el-form-item>
        <el-form-item label="流程标识" prop="workflowCode">
          <el-input v-model="form.workflowCode" placeholder="请输入流程标识" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态" style="width:100%">
            <el-option label="全部" value />
            <el-option label="停用" value="0" />
            <el-option label="正常" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import { listWorkflowConfig, getWorkflowConfig, delWorkflowConfig, addWorkflowConfig, updateWorkflowConfig, exportWorkflowConfig } from "@/api/system/workflowConfig";

export default {
  name: "WorkflowConfig",
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 流程配置表格数据
      workflowConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workflowName: undefined,
        workflowCode: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "id 主键不能为空", trigger: "blur" }
        ],
        workflowName: [
          { required: true, message: "流程名称不能为空", trigger: "blur" }
        ],
        url: [
          { required: true, message: "流程链接不能为空", trigger: "blur" }
        ],
        workflowCode: [
          { required: true, message: "流程标识不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态（0停用，1正常）不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询流程配置列表 */
    getList () {
      this.loading = true;
      listWorkflowConfig(this.queryParams).then(response => {
        this.workflowConfigList = response.rows.map(item => {
          item.statusText = item.status === "0" ? '停用' : '正常'
          return item
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}系统管理/成果校审流程清单.xlsx`
      )}&name=${encodeURIComponent("成果校审流程清单.xlsx")}`;
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        workflowName: undefined,
        url: undefined,
        workflowCode: undefined,
        status: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        guid: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.guid)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加流程配置";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const guid = row.guid || this.ids
      getWorkflowConfig(guid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流程配置";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guid != undefined) {
            updateWorkflowConfig(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addWorkflowConfig(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const guids = row.guid || this.ids;
      this.$confirm('是否确认删除流程配置数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delWorkflowConfig(guids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport () {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有流程配置数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportWorkflowConfig(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () { });
    }
  }
};
</script>
<style lang="scss" scoped>
.title-icon {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  cursor: pointer;
  margin-bottom: 10px;
  .iconhelp {
    font-size: 18px;
  }
}
</style>